package other;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Map;

/**
 * 20.有效的括号
 */
public class Solution {
    public static void main(String[] args) {
        String content = "{[}";
        boolean ret = isValid(content);
        System.out.println("output:" + ret);
    }

    public static boolean isValid(String content) {
        Map<Character, Character> pairs = Map.of(
                ']', '[',
                '}', '{',
                ')', '(');
        Deque<Character> stack = new LinkedList<>();
        for (int i = 0; i < content.length(); i++) {
            Character ch = content.charAt(i);
            if (pairs.containsKey(ch)) {
                if (stack.isEmpty() || stack.peek() != pairs.get(ch)) {
                    return false;
                }
                stack.pop();
            } else {
                stack.push(ch);
            }

            System.out.println(stack);
        }

        return stack.isEmpty();
    }
}
